//
//  JYStatus.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/15.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit

class JYStatus: NSObject {

//    微博创建时间
    var created_at: String?
//    微博ID
    var id: Int64 = 0
//    微博信息内容
    var text: String?
//    微博来源
    var source: String?
//    图片的数组,里面保存的是字典
    var pic_urls:[[String: AnyObject]]? {
        didSet{
        
            let count = pic_urls?.count ?? 0
            //没图片
            if count == 0 {
            return
            }
             storePicutreURLs = [NSURL]()
            //有图片显示
            for dict in pic_urls! {
            let urlString = dict["thumbnail_pic"] as! String
            let picURL = NSURL(string: urlString)!
            storePicutreURLs?.append(picURL)
            }
            
        }
    }
    /// 模型提供好用一点的图片NSURL数组
    var storePicutreURLs: [NSURL]?
    
    //    用户模型                                                  提示: var user: JYUser?   !!!
    var user: JYUser?
    
    //被转发的原微博信息字段，当该微博为转发微博时返回
    var retweeted_status: JYStatus?
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
            user = JYUser(dict: dict)
            }
            return
        }
        
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
                retweeted_status = JYStatus(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    //防爆
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
//        JYPrint("forUndefinedKey : \(key)")
    }
    
    // 注意这是个变量 对比下面的方法
    override var description: String {
        get {
            //解析: 根据key 拿到对应的值,然后拼成字典 ,所以一个字典中有 5 对
            let keys = ["created_at", "id", "text", "source", "pic_urls","user","retweeted_status"]
        return " 微博模型: \(self.dictionaryWithValuesForKeys(keys).description)"
        }
    }
    //如果不记得,在CustomStringConvertible协议中找
//    override var description: String {
//        get {
//            return "access_token: \(access_token) ,expires_in: \(expires_in) ,uid: \(uid), expires_inDate: \(expires_inDate) screen_name:\(screen_name), avatar_large:\(avatar_large) "
//        }
//    }
    
}
